#!/usr/bin/env python
#
# Copyright (C) 2011 Clement Lorteau <clem@lorteau.fr>	
#		
# This file is part of QuickScreenShots.		
# 	
# This program is free software; you can redistribute it and/or modify	
# it under the terms of the GNU General Public License as published by		
# the Free Software Foundation; version 3.	
# 	
# This program is distributed in the hope that it will be useful,		
# but WITHOUT ANY WARRANTY; without even the implied warranty of		
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		
# GNU General Public License for more details.		
# 		
# You should have received a copy of the GNU General Public License		
# along with this program; if not, write to the Free Software		
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import wx
import wx.lib.newevent

spin_button_changed_evt, EVT_SPIN_BUTTON_CHANGED = wx.lib.newevent.NewCommandEvent()

class SpinButtonChangedEvent(spin_button_changed_evt):
    def __init__(self, value, id):
        self.value = value
        spin_button_changed_evt.__init__(self, id)
        
    def GetValue(self):
        return self.value

class IntButton(wx.Button):
    """
    A button showing an integer. Left click or mouse wheel up increases the value,
    right click or mouse wheel down decreases the value.
    """
    def __init__(self, parent, id=-1, min=0, max=99, value=None):
        self.min = min
        self.max = max
        self.parent = parent
        if value:
            self.value = value
        else:
            self.value = min
        self.value = "%d" % self.value
        wx.Button.__init__(self, parent, id, self.value)
        
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        bigfont = wx.Font(font.GetPointSize()+3, font.GetFamily(), font.GetStyle(),
                          wx.FONTWEIGHT_BOLD, font.GetUnderlined(), font.GetFaceName(),
                          font.GetEncoding())
        self.SetFont(bigfont)
        
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeft)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRight)
        self.Bind(wx.EVT_RIGHT_DCLICK, self.OnRight)
        self.Bind(wx.EVT_MOUSEWHEEL, self.OnWheel)
        self.Bind(wx.EVT_ENTER_WINDOW, self.OnEnter)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
        
    def SetValue(self, value):
        """
        Value has to be an int
        """
        self.value = value
        wx.Button.SetLabel(self, "%d" % value)
        self.Changed()
        
    def GetValue(self):
        """
        Returns an int
        """
        return int(self.value)
        
    def Increment(self):
        if self.GetValue() != self.max:
            self.SetValue(self.GetValue() + 1)
        
    def Decrement(self):
        if self.GetValue() != self.min:
            self.SetValue(self.GetValue() - 1)
            
    def Changed(self):
        """Fire the value changed event"""
        evt = SpinButtonChangedEvent(self.value, -1)
        self.GetEventHandler().ProcessEvent(evt)
        
    def OnLeft(self, event):
        self.Increment()
        event.Skip()
        
    def OnRight(self, event):
        self.Decrement()
        event.Skip()
        
    def OnWheel(self, event):
        rot = event.GetWheelRotation()
        if rot < 0:
            self.Decrement()
        if rot > 0:
            self.Increment()
            
    def OnEnter(self, event):
        self.SetFocus()
        event.Skip()
        
    def OnLeave(self, event):
        self.GetParent().SetFocus()
        event.Skip()

if __name__ == '__main__':

    def OnChanged(event):
        print event.GetValue()
        
    app = wx.PySimpleApp(0)
    frame = wx.Frame(None, title='', style=wx.DEFAULT_FRAME_STYLE)
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    thebutton = IntButton(frame)
    frame.Bind(EVT_SPIN_BUTTON_CHANGED, OnChanged)
    sizer.Add(thebutton)
    frame.SetSizer(sizer)
    frame.Show()
    app.SetTopWindow(frame)
    app.MainLoop()